package com.mysiteforme.admin.controller.publish;

import com.mysiteforme.admin.service.ElectricityMessagePushService;
import com.xiaoleilu.hutool.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.mysiteforme.admin.util.LayerData;
import com.mysiteforme.admin.util.RestResponse;
import com.mysiteforme.admin.util.ToolUtil;
import com.mysiteforme.admin.annotation.SysLog;
import com.mysiteforme.admin.base.MySysUser;
import com.mysiteforme.admin.dao.publish.ElectricitySupplyDao;
import com.mysiteforme.admin.entity.publish.ElectricitySupply;
import com.mysiteforme.admin.service.publish.ElectricitySupplyService;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 供需信息表  前端控制器
 * </p>
 *
 * @author wangl
 * @since 2019-09-18
 */
@Controller
@RequestMapping("/admin/electricitySupply")
public class ElectricitySupplyController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ElectricitySupplyController.class);

    @Autowired
    private ElectricitySupplyService electricitySupplyService;
    @Autowired
    private ElectricitySupplyDao electricitySupplyMapper;
    @Autowired
    private ElectricityMessagePushService electricityMessagePushService;

    @GetMapping("list")
    @SysLog("跳转供需信息表列表")
    public String list(){
        return "/admin/electricitySupply/list";
    }

    @PostMapping("list")
    @ResponseBody
    @SysLog("请求供需信息表列表数据")
    public LayerData<ElectricitySupply> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<ElectricitySupply> layerData = new LayerData<>();

        map.put("page",page);
        map.put("limit",limit);
        Page<ElectricitySupply> pageData = electricitySupplyService.selectSupplyList(map);
        //省市编码转中文描述
        List<ElectricitySupply> records = pageData.getRecords();
        for (ElectricitySupply electricitySupply : records) {
        	if(electricitySupply.getCity()==null)
        		continue;
        	String areaName = electricitySupplyMapper.getAreaNameById(electricitySupply.getCity());
        	electricitySupply.setCity(areaName);
        	electricitySupply.setStringId(String.valueOf(electricitySupply.getId()));
		}
        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @GetMapping("add")
    @SysLog("跳转新增供需信息表页面")
    public String add(){
        return "/admin/electricitySupply/add";
    }

    @PostMapping("add")
    @SysLog("保存新增供需信息表数据")
    @ResponseBody
    public RestResponse add(ElectricitySupply electricitySupply){
        if(StringUtils.isBlank(electricitySupply.getSupplyId())){
            return RestResponse.failure("主键id不能为空");
        }
        if(StringUtils.isBlank(electricitySupply.getUserId())){
            return RestResponse.failure("成员id不能为空");
        }
        if(StringUtils.isBlank(electricitySupply.getUserType())){
            return RestResponse.failure("成员类型（继承于成员表）不能为空");
        }
        if(StringUtils.isBlank(electricitySupply.getCity())){
            return RestResponse.failure("所在地（继承于成员表）不能为空");
        }
        electricitySupplyService.insert(electricitySupply);
        return RestResponse.success();
    }

    @GetMapping("edit")
    @SysLog("跳转编辑供需信息表页面")
    public String edit(Long id,Model model){
        ElectricitySupply electricitySupply = electricitySupplyService.selectById(id);
        model.addAttribute("electricitySupply",electricitySupply);
        return "/admin/electricitySupply/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    @SysLog("保存编辑供需信息表数据")
    public RestResponse edit(ElectricitySupply electricitySupply){
        if(null == electricitySupply.getId() || 0 == electricitySupply.getId()){
            return RestResponse.failure("ID不能为空");
        }
        if(StringUtils.isBlank(electricitySupply.getSupplyId())){
            return RestResponse.failure("主键id不能为空");
        }
        if(StringUtils.isBlank(electricitySupply.getUserId())){
            return RestResponse.failure("成员id不能为空");
        }
        if(StringUtils.isBlank(electricitySupply.getUserType())){
            return RestResponse.failure("成员类型（继承于成员表）不能为空");
        }
        if(StringUtils.isBlank(electricitySupply.getCity())){
            return RestResponse.failure("所在地（继承于成员表）不能为空");
        }
        electricitySupplyService.updateById(electricitySupply);
        return RestResponse.success();
    }

    @PostMapping("delete")
    @ResponseBody
    @SysLog("删除供需信息表数据")
    public RestResponse delete(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        ElectricitySupply electricitySupply = electricitySupplyService.selectById(id);
        electricitySupply.setDelFlag(true);
        electricitySupplyService.updateById(electricitySupply);
        return RestResponse.success();
    }
    
    @PostMapping("pass")
    @ResponseBody
    @SysLog("供需信息审核通过")
    public RestResponse pass(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        ElectricitySupply electricitySupply = electricitySupplyService.selectById(id);
        electricitySupply.setAuditingStatu("1");
        electricitySupply.setUpdateDate(new Date());
        electricitySupply.setUpdateId(MySysUser.id());
        electricitySupplyService.updateById(electricitySupply);
//        electricityMessagePushService.insertSupplyMessage(electricitySupply,electricitySupply.getUserId());
        return RestResponse.success();
    }
    
    @PostMapping("refuse")
    @ResponseBody
    @SysLog("供需信息审核拒绝")
    public RestResponse refuse(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        ElectricitySupply electricitySupply = electricitySupplyService.selectById(id);
        electricitySupply.setAuditingStatu("2");
        electricitySupply.setUpdateDate(new Date());
        electricitySupply.setUpdateId(MySysUser.id());
        electricitySupplyService.updateById(electricitySupply);
//        electricityMessagePushService.insertSupplyMessage(electricitySupply,electricitySupply.getUserId());
        return RestResponse.success();
    }

}
